/* ========================================
 *
 * Copyright YOUR COMPANY, THE YEAR
 * All Rights Reserved
 * UNPUBLISHED, LICENSED SOFTWARE.
 *
 * CONFIDENTIAL AND PROPRIETARY INFORMATION
 * WHICH IS THE PROPERTY OF your company.
 *
 * ========================================
*/

#include <project.h>
#include "userMain.h"
#include "userRelayDriver.h"

//#define     RELAY_DEBUG

void userFirstLoadFunc(unsigned char userOperateMode);
void userSecondLoadFunc(unsigned char userOperateMode);
void userThirdLoadFunc(unsigned char userOperateMode);
void userFourthLoadFunc(unsigned char userOperateMode);

unsigned char userRelayOperateMode=0;
//unsigned char userRelayModeCurrent = 0;
//unsigned char userRelayModeNow = 0;
unsigned char userRelayLowPowerFlag = 0;
unsigned char userRelayOperateSrc = 0;
unsigned char userRelayStateNow=0;
uint8 uRelayStep = 0;
uint8 timerzero = 0;

void userRelayInitFunc(void)
{
    userRelayChangePowerFunc(0x00);
    uRelayStep = 0;
    userRelayStateNow = 0;
    userRelayLowPowerFlag = 0;
}
//uint8 userRelayPowerChangeFlag = 0;
uint16 userRelayChangeCnt = 0;
void userRelayChangeTickFunc(void)
{
    if(userRelayChangeCnt < 20000)
        userRelayChangeCnt++;
}
void userRelayLowPowerFunc(void)
{
    if(userRelayLowPowerFlag) return ;
    if(userRelayChangeCnt < 5000) return ;
    userRelayChangeCnt = 0;
    userRelayChangePowerFunc(userRelayOperateMode);
}

extern unsigned char userRelayOperateState;
void userRelayCtrlFunc(unsigned char userOperateSrc, unsigned char userOperateMode)
{
    if(userOperateSrc == 1)// uart 
    {
        userRelayOperateSrc = 1;
    }
    else if(userOperateSrc == 2)//keypad
    {
        userRelayOperateSrc = 2;
    }
    else
    {
       userRelayOperateSrc = 3;
    }
    if(userRelayLowPowerFlag)
    {
        userRelayOperateMode = userRelayStateNow >> 4;
    }
    else
    {
        userRelayOperateMode = userRelayStateNow;
    }
    _NOP;
    userFirstLoadFunc(userOperateMode);
    userSecondLoadFunc(userOperateMode);
    userThirdLoadFunc(userOperateMode);
    //userFourthLoadFunc(userOperateMode);
    _NOP;
    uRelayStep = 1;
    userLedProcessFunc();
}

extern uint8 uZeroLevel;
void userRelayPowerSetFunc(void)
{
    static uint8 uRelaySetCnt=0;
    if(uRelayStep==0) return ;
    switch(uRelayStep)
    {
        case 0x01:
        {
            uZeroLevel &= 0xEF;
            uRelayStep = 0x02;
			timerzero = 0;
            break;
        }
        case 0x02:
        {
            if(uZeroLevel & 0x10)
            {
                uRelayStep = 0x03;
                uRelaySetCnt = 0;
            }
			else
				{
				if(timerzero>=100)
					uRelayStep = 0x03;




				}
            #ifdef RELAY_DEBUG
                uRelayStep = 0x03;
            #endif
            break;
        }
         case 0x03:
        {
            uRelaySetCnt++;
            if(uRelaySetCnt >= 9)
            {
                userRelayPowerCtrlFunc(userRelayOperateMode);
                userSendRelayInfoFunc();
                uRelayStep = 0x00;
            }
            break;
        }
         default:
        {
            break;
        }
    }
}

extern unsigned char userLedStateTemp;
extern unsigned char userLedState;
void userRelayPowerCtrlFunc(unsigned char userRelayMode)//Relay High Power Ctrl 
{
    if(userRelayMode & 0x01)    RELAY_SW1_Write(1);
     else  RELAY_SW1_Write(0);
    if(userRelayMode & 0x02)    RELAY_SW2_Write(1);
     else  RELAY_SW2_Write(0);
    if(userRelayMode & 0x04)    RELAY_SW3_Write(1);
     else  RELAY_SW3_Write(0);
    if(userRelayMode & 0x08)    _NOP;
    userRelayStateNow = userRelayMode;
    userRelayLowPowerFlag = 0;
    userRelayChangeCnt = 0;
    RELAY_ST1_Write(0);
    RELAY_ST2_Write(0);
    RELAY_ST3_Write(0);
    userLedStateTemp = userLedState & 0xF0;
    userLedStateTemp |= userRelayMode & 0x07;
    userLedState = userLedStateTemp;
    LED_Write(userLedStateTemp);
}

void userRelayChangePowerFunc(unsigned char userRelayMode)//Relay Low Power
{   
    if(userRelayMode & 0x01)   RELAY_ST1_Write(1);
      else  RELAY_ST1_Write(0);
    if(userRelayMode & 0x02)    RELAY_ST2_Write(1);
     else   RELAY_ST2_Write(0);
    if(userRelayMode & 0x04)    RELAY_ST3_Write(1);
     else   RELAY_ST3_Write(0);
    if(userRelayMode & 0x08)    _NOP;
    userRelayStateNow = userRelayMode << 4;
    userRelayLowPowerFlag = 1;
    RELAY_SW1_Write(0);
    RELAY_SW2_Write(0);
    RELAY_SW3_Write(0);
}

extern unsigned char uLoadDelayCloseFlag;
void userFirstLoadFunc(unsigned char userOperateMode)
{
    unsigned char userOperateModeTemp;
    userOperateModeTemp = userOperateMode & 0x03;// the first Load 
    if(userOperateModeTemp != 0x02)uLoadDelayCloseFlag &= 0xFE;
    //CyDelay(5);
    switch(userOperateModeTemp)
    {
        case 0x00:  //
        {
            userRelayOperateMode &= 0xFE;
            if(userRelayOperateSrc !=3) userRelayOperateState = 0x00;
            //userLedCtrlFunc(5,0); 
            //userLedProcessFunc();
            break;
        }
        case 0x01:
        {
            userRelayOperateMode |= 0x01;
            if(userRelayOperateSrc !=3) userRelayOperateState = 0x01;
            //userLedCtrlFunc(5,1); 
            //userLedProcessFunc();
            break;
        }
        case 0x03:
        {
            if(userRelayOperateMode & 0x01)
            {
                userRelayOperateMode &= 0xFE;
                if(userRelayOperateSrc !=3) userRelayOperateState = 0x00;
                //userLedCtrlFunc(5,0); 
                //userLedProcessFunc();
            }
            else
            {
                userRelayOperateMode |= 0x01;
                if(userRelayOperateSrc !=3) userRelayOperateState = 0x01;
               // userLedCtrlFunc(5,1); 
                //userLedProcessFunc();
            }
            break;
        }
        default:
        {
            break;
        }
    }
}

void userSecondLoadFunc(unsigned char userOperateMode)
{
    unsigned char userOperateModeTemp; 
    userOperateModeTemp = userOperateMode & 0x0C; //the second load
    if(userOperateModeTemp != 0x08)uLoadDelayCloseFlag &= 0xFD;
    switch(userOperateModeTemp)
    {
        case 0x00:  //
        {
            userRelayOperateMode &= 0xFD;
            //userLedCtrlFunc(6,0);
            //userLedProcessFunc();
            break;
        }
        case 0x04:
        {
            userRelayOperateMode |= 0x02;
            //userLedCtrlFunc(6,1);
            //userLedProcessFunc();
            break;
        }
        case 0x0c:
        {
            if(userRelayOperateMode & 0x02)
            {
                userRelayOperateMode &= 0xFD;
                //userLedCtrlFunc(6,0);
                //userLedProcessFunc();
            }
            else
            {
                userRelayOperateMode |= 0x02;
                //userLedCtrlFunc(6,1);
                //userLedProcessFunc();
            }
            break;
        }
        default:
        {
            break;
        }
    }
}

void userThirdLoadFunc(unsigned char userOperateMode)
{
    unsigned char userOperateModeTemp; 
    userOperateModeTemp = userOperateMode & 0x30;
    if(userOperateModeTemp != 0x20)uLoadDelayCloseFlag &= 0xFB;
    switch(userOperateModeTemp)
    {
        case 0x00:  //
        {
            userRelayOperateMode &= 0xFB;
            //userLedCtrlFunc(7,0);
            //userLedProcessFunc();
            break;
        }
        case 0x10:
        {
            userRelayOperateMode |= 0x04;
            //userLedCtrlFunc(7,1);
            //userLedProcessFunc();
            break;
        }
        case 0x30:
        {
            if(userRelayOperateMode & 0x04)
            {
                userRelayOperateMode &= 0xFB;
                //userLedCtrlFunc(7,0);
                //userLedProcessFunc();
            }
            else
            {
                userRelayOperateMode |= 0x04;
                //userLedCtrlFunc(7,1);
                //userLedProcessFunc();
            }
            break;
        }
        default:
        {
            break;
        }
    }
}

void userFourthLoadFunc(unsigned char userOperateMode)
{
    unsigned char userOperateModeTemp;
    userOperateModeTemp = userOperateMode & 0xC0;
    switch(userOperateModeTemp)
    {
        case 0x00:  //
        {
            userRelayOperateMode &= 0xF7;
            break;
        }
        case 0x40:
        {
            userRelayOperateMode |= 0x08;
            break;
        }
        case 0xC0:
        {
            if(userRelayOperateMode & 0x08)
            {
                userRelayOperateMode &= 0xF7;
            }
            else
            {
                userRelayOperateMode |= 0x08;
            }
            break;
        }
        default:
        {
            break;
        }
    }
}


/* [] END OF FILE */
